package com.xintonghua.bussiness.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xintonghua.base.adapter.BaseAdapter;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.bean.GoodsManageBean;
import com.xintonghua.bussiness.util.GlideUtils;
import com.xintonghua.bussiness.util.MyUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liqi on 2018/4/1.
 */

public class SearchProductAdapter extends BaseAdapter<GoodsManageBean, SearchProductAdapter.GoodViewHolder> {
    private boolean check = false;

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

    public SearchProductAdapter(Activity mActivity, List<GoodsManageBean> list) {
        super(mActivity, list);
    }


    @Override
    public SearchProductAdapter.GoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_goods, parent, false);
        return new SearchProductAdapter.GoodViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SearchProductAdapter.GoodViewHolder holder, int position) {
        final GoodsManageBean bean = list.get(position);
        GlideUtils.load(mActivity, holder.ivGoodsHead, bean.getImgs());
        holder.tvGoodsName.setText(bean.getName());
        holder.tvGoodsPrice.setText(MyUtils.getMoney(mActivity, bean.getPrice()));
        holder.tvGoodsCount.setVisibility(isCheck() ? View.GONE : View.VISIBLE);
        holder.tvGoodsCount.setText("剩余库存" + bean.getInventory() + "件");

        holder.rlGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("data", bean);
                mActivity.setResult(Activity.RESULT_OK, intent);
                mActivity.finish();
            }
        });
    }


    class GoodViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_goods_head)
        ImageView ivGoodsHead;
        @BindView(R.id.tv_goods_name)
        TextView tvGoodsName;
        @BindView(R.id.tv_goods_price)
        TextView tvGoodsPrice;
        @BindView(R.id.tv_goods_count)
        TextView tvGoodsCount;
        @BindView(R.id.rl_goods)
        RelativeLayout rlGoods;

        public GoodViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}